﻿Imports SACommon
Imports LiteDB

Public Class UpdateApp

    Private mItems As List(Of UpdateAppItem)
    Private mSelectedAppID As Integer
    Private mApplicationStatus As AppStatus
#Region "Events"
    Event ApplicationStatusChanged(Status As AppStatus)
#End Region
#Region "Get/Set Properties"
    Property SelectedAppID() As Integer
        Get
            Return mSelectedAppID
        End Get
        Set(value As Integer)
            mSelectedAppID = value
            SetAppStatus()
        End Set
    End Property
    Property SelectedApp As UpdateAppItem
        Get
            Try
                Return (From x In mItems Where x.ApplicationID = SelectedAppID).First
            Catch
                Return New UpdateAppItem
            End Try
        End Get
        Set(value As UpdateAppItem)
            mSelectedAppID = value.ApplicationID
            Try
                Dim a = (From x In mItems Where x.ApplicationID = SelectedAppID).First
                a = value
            Catch ex As Exception
                mItems.Add(value)
            End Try
        End Set
    End Property
    Property NewFileStatus As SAEventArgs.StatusCodes
        Get
            Return SelectedApp.FileStatus
        End Get
        Set(value As SAEventArgs.StatusCodes)
            SelectedApp.FileStatus = value
            SetAppStatus()
        End Set
    End Property
    Property NewVersionStatus As SAEventArgs.StatusCodes
        Get
            Return SelectedApp.VersionStatus
        End Get
        Set(value As SAEventArgs.StatusCodes)
            SelectedApp.VersionStatus = value
            SetAppStatus()
        End Set
    End Property
#End Region
#Region "Get Properties"
    Public ReadOnly Property ApplicationsList As List(Of UpdateAppItem)
        Get
            Return mItems
        End Get
    End Property
    Public ReadOnly Property ApplicationsList(SelectedCategoryID As Integer) As List(Of UpdateAppItem)
        Get
            Return (From x In mItems Where x.CategoryID = SelectedCategoryID).ToList
        End Get
    End Property
    Public ReadOnly Property GetApplication(SelectedApplicationID As Integer) As UpdateAppItem
        Get
            Try
                Return (From x In mItems Where x.ApplicationID = SelectedApplicationID).First
            Catch
                Return New UpdateAppItem
            End Try
        End Get
    End Property
    ReadOnly Property SelectedAppStatus As AppStatus
        Get
            Return mApplicationStatus
        End Get
    End Property
#End Region
#Region "Functions"
    Sub New()
        mItems = New List(Of UpdateAppItem)
        LoadList()
    End Sub
    Private Sub LoadList()
        Using db As New LiteEngine(gApp.DatabaseFile)
            Dim col = db.GetCollection(Of UpdateAppItem)(DTApplications)
            mItems = (From x In col.All Order By x.ApplicationTitle).ToList
        End Using
    End Sub
    Function Save(Data As UpdateAppItem) As Integer
        Dim tAdd As Boolean
        Dim n As UpdateAppItem
        mSelectedAppID = Data.ApplicationID

        Using db As New LiteEngine(gApp.DatabaseFile)
            Dim col = db.GetCollection(Of UpdateAppItem)(DTApplications)

            tAdd = False
            If mSelectedAppID = 0 Then
                Try
                    mSelectedAppID = (From x In col.All Select x.ApplicationID).Max + 1
                Catch ex As Exception
                    mSelectedAppID = 1
                End Try
                Data.ApplicationID = mSelectedAppID
                tAdd = True
            End If

            If tAdd = False Then
                n = col.FindById(Data.ApplicationID)
                Dim a = (From x In mItems Where x.ApplicationID = Data.ApplicationID).First
                mItems.Remove(a)
            Else
                n = New UpdateAppItem
                n.ApplicationID = Data.ApplicationID
            End If

            n.ApplicationTitle = Data.ApplicationTitle
            n.RepositoryServerURL = Data.RepositoryServerURL
            n.FileStatus = Data.FileStatus
            n.VersionStatus = Data.VersionStatus
            n.CategoryID = Data.CategoryID
            n.DirectoryList = Data.DirectoryList
            n.InstallerVersion = Data.InstallerVersion
            n.InstallerMD5 = Data.InstallerMD5
            n.FTPServerURL = Data.FTPServerURL
            n.FTPUsername = Data.FTPUsername
            n.FTPPassword = Data.FTPPassword
            n.HelpMD5 = Data.HelpMD5
            n.HelpVersion = Data.HelpVersion
            n.RepositoryFolder = Data.RepositoryFolder
            n.DateTimeCreated = DateFunctions.GetDateTimeString(Now)

            If tAdd = True Then
                col.Insert(n)
            Else
                col.Update(n)
            End If
            mItems.Add(n)
        End Using
        Return n.ApplicationID
    End Function
    Function GetAppStatus(AppItem As UpdateAppItem) As AppStatus
        Dim tStatus As AppStatus

        If AppItem.FileStatus = SAEventArgs.StatusCodes.NewItem Then tStatus = AppStatus.NewApp
        If AppItem.VersionStatus = SAEventArgs.StatusCodes.NewItem Then tStatus = AppStatus.NewApp
        If AppItem.FileStatus = SAEventArgs.StatusCodes.Defined Then
            If AppItem.VersionStatus = SAEventArgs.StatusCodes.Defined Then
                tStatus = AppStatus.DefinedApp
            ElseIf AppItem.VersionStatus = SAEventArgs.StatusCodes.Uploaded Then
                tStatus = AppStatus.PublishedApp
            End If
        End If

        Return tStatus
    End Function
    Sub Remove(Data As UpdateAppItem)
        Using db As New LiteEngine(gApp.DatabaseFile)
            Dim col = db.GetCollection(Of UpdateAppItem)(DTApplications).Delete(Data.ApplicationID)
        End Using
    End Sub
    Sub SetAppStatus()
        Dim tCurrentStatus As AppStatus = mApplicationStatus

        If SelectedApp.FileStatus = SAEventArgs.StatusCodes.NewItem Then mApplicationStatus = AppStatus.NewApp
        If SelectedApp.VersionStatus = SAEventArgs.StatusCodes.NewItem Then mApplicationStatus = AppStatus.NewApp
        If SelectedApp.FileStatus = SAEventArgs.StatusCodes.Defined Then
            If SelectedApp.VersionStatus = SAEventArgs.StatusCodes.Defined Then
                mApplicationStatus = AppStatus.DefinedApp
            ElseIf SelectedApp.VersionStatus = SAEventArgs.StatusCodes.Uploaded Then
                mApplicationStatus = AppStatus.PublishedApp
            End If
        End If

        If mApplicationStatus <> tCurrentStatus Then RaiseEvent ApplicationStatusChanged(mApplicationStatus)
    End Sub
#End Region
    Enum AppStatus
        NewApp = 1
        DefinedApp = 2
        PublishedApp = 3
    End Enum
End Class
